package com.circle.rest.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.model.ErrorResponseData;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.sys.model.Menu;
import com.circle.rest.modular.sys.model.User;
import com.circle.rest.modular.sys.service.IMenuService;
import com.circle.rest.modular.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-02
 */
@Api(value = "菜单信息", tags = {"sys : 菜单信息API"})
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IUserService userService;

    /**
     * 查询菜单信息列表
     */
    @ApiOperation("查询菜单信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "菜单名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "Integer")
    })
    @Log(title = "查询菜单信息列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(String name, Integer status) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        if (ToolUtil.isNotEmpty(status)) {
            wrapper = wrapper.eq("status", status);
        }
        if (ToolUtil.isNotEmpty(name)) {
            wrapper = wrapper.like("name", name);
        }

        List<Map<String, Object>> maps = menuService.listMaps(wrapper);
        return new SuccessResponseData(maps);
    }

    /**
     * 添加菜单信息
     */
    @ApiOperation("添加菜单信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Log(title = "添加菜单信息", businessType = BusinessType.INSERT)
    @Permission
    public Object add(Menu menu) {

        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("url", menu.getUrl());
        List<Menu> list = menuService.list(wrapper);
        if (list.size() > 0) {
            throw new ServiceException("请求地址已经存在");
        }

        menu.setUpdateTime(LocalDateTime.now());
        menu.setCreateTime(LocalDateTime.now());
        boolean save = menuService.save(menu);
        return new SuccessResponseData(save);
    }

    /**
     * 修改菜单信息
     */
    @ApiOperation("修改菜单信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Log(title = "修改菜单信息", businessType = BusinessType.UPDATE)
    @Permission
    public Object edit(Menu menu) {
        if (ToolUtil.isEmpty(menu.getId())) {
            return new ErrorResponseData("编号不能为空");
        }
        if (ToolUtil.isEmpty(menu.getVersion())) {
            return new ErrorResponseData("版本号不能为空");
        }
        menu.setUpdateTime(LocalDateTime.now());
        if (!menuService.updateById(menu)) {
            throw new ServiceException("更新失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除菜单信息
     */
    @ApiOperation("删除菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除菜单信息", businessType = BusinessType.DELETE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("菜单信息编号不能为空");
        }
        Menu menuOld = menuService.getById(id);
        if (ToolUtil.isEmpty(menuOld)) {
            throw new ServiceException("菜单信息不存在，删除失败");
        }
        if (!menuService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询菜单信息
     */
    @ApiOperation("查询菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询菜单信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("菜单信息编号不能为空");
        }
        Menu menu = menuService.getById(id);
        if (ToolUtil.isEmpty(menu)) {
            throw new ServiceException("菜单信息不存在");
        }
        return new SuccessResponseData(menu);
    }

    /**
     * 根据用户编号获取菜单信息
     */
    @ApiOperation("根据用户编号获取菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "根据用户编号获取菜单信息", required = true, dataType = "String")
    })
    @RequestMapping(value = "/getMenusByUserId", method = RequestMethod.GET)
    @Log(title = "根据用户编号获取菜单信息", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object getMenusByUserId(String userId) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException("用户编号不能位空");
        }
        User user = userService.getById(userId);
        if (user == null) {
            throw new ServiceException("找不到用户");
        }
        String roleid = user.getRoleid();

        List<Map<String, Object>> menus = menuService.getMenusByRoleId(roleid);

        return new SuccessResponseData(menus);
    }
}
